<?php

class MySqlConnect
{
    public function GetConn()
    {
		//外网
        $con = mysqli_connect("jkera2016.mysql.rds.aliyuncs.com", "jkroot", "Jkera2016", "ys");
//      $con = mysqli_connect("127.0.0.1", "root", "Jkera2016", "test");
        $con->query("set names utf8");
        return $con;
    }

    public function ExecuteData($sql)
    {
        $con = $this->GetConn();
        if ($con->query($sql)) {
        		mysqli_close($con);
            return 1;
        }else{
       	 	mysqli_close($con);
            return 0;
        }
    }
	


    
	public function GetDataSetJson($sql)
    {
        $con = $this->GetConn();
        $result = $con->query($sql);
        if ($result) {
            $users = array();
            $i = 0;
            $fields = $result->fetch_fields();
            $length = count($fields);
            $count = $result->num_rows;
            $json = "";
            $json .= "{\"data1\":[";
            while ($row = $result->fetch_assoc()) {
                $json .= "{";
                for ($i = 0; $i <= $length - 1; $i++) {
                    $ziduan = $fields[$i]->name;
                    $json .= "\"" . $ziduan . "\":\"" . $row[$ziduan] . "\"";
                    if ($i < $length - 1) {
                        $json .= ",";
                    }
                }
                $json .= "}";
                if ($count > 1) {
                    $count--;
                    $json .= ",";
                }
            }
            $json .= "]}";
			mysqli_close($con);
            return $json;
            
        }
    }
	
	public function GetDataSetJsonPHP($sql)
    {
        $con = $this->GetConn();
        $result = $con->query($sql);
        if ($result) {
            $users = array();
            $i = 0;
            $fields = $result->fetch_fields();
            $length = count($fields);
            $count = $result->num_rows;
            $json = "";
            $json .= "[";
            while ($row = $result->fetch_assoc()) {
                $json .= "{";
                for ($i = 0; $i <= $length - 1; $i++) {
                    $ziduan = $fields[$i]->name;
                    $json .= "\"" . $ziduan . "\":\"" . $row[$ziduan] . "\"";
                    if ($i < $length - 1) {
                        $json .= ",";
                    }
                }
                $json .= "}";
                if ($count > 1) {
                    $count--;
                    $json .= ",";
                }
            }
            $json .= "]";
			mysqli_close($con);
            return $json;
            
        }
    }

	public function getImg($url = "", $filename = "") {
	    if(is_dir(basename($filename))) {
	        echo "The Dir was not exits";
	        return false;
	    }
	    //去除URL连接上面可能的引号
//	    $url = preg_replace( '/(?:^['"]+|['"/]+$)/', '', $url );
	    $hander = curl_init();
	    $fp = fopen($filename,'wb');
	    curl_setopt($hander,CURLOPT_URL,$url);
	    curl_setopt($hander,CURLOPT_FILE,$fp);
	    curl_setopt($hander,CURLOPT_HEADER,0);
	    curl_setopt($hander,CURLOPT_FOLLOWLOCATION,1);
	    //curl_setopt($hander,CURLOPT_RETURNTRANSFER,false);//以数据流的方式返回数据,当为false是直接显示出来
	    curl_setopt($hander,CURLOPT_TIMEOUT,60);
	    /*$options = array(
	        CURLOPT_URL=> '/thum-f3ccdd27d2000e3f9255a7e3e2c4880020110622095243.jpg',
	        CURLOPT_FILE => $fp,
	        CURLOPT_HEADER => 0,
	        CURLOPT_FOLLOWLOCATION => 1,
	        CURLOPT_TIMEOUT => 60
	    );
	    curl_setopt_array($hander, $options);
	    */
	    curl_exec($hander);
	    curl_close($hander);
	    fclose($fp);
	    return  true;
	}

}
?>